/************************************************************************************************      
* Name:  SaveCOIPerson                                                                  
* Purpose: Stored procedure for updating or inserting a person (With respect COI)    
  Note: The following changes is applied to the Update Person Table.    
 1.The "Add" or "Update" of "Title" will be carried out on the AdmPerson Table instead of the admExternalPerson.(The "Title" field from admExternalPerson will not be used and should be removed)        
  --The update will be performed only on two fields(Outsidemamber flag and the COIRole)from COI.    
* PARAMETERS        
* Name   Description      
* ------------------------- -----------------------------      
* @admPersonId  AdmPerson Id      
*                
************************************************************************************************/      

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SaveCOIPerson]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SaveCOIPerson]
GO

----default @HierarchyTypeId = 3 which is Institutional type.      
CREATE procedure dbo.SaveCOIPerson      
(      
 @admPersonId int,        
 @lastName varchar(64),      
 @firstName varchar(64),      
 @admPersonType int,      
 @orgId int = 0,      
 @orgName varchar(200),      
 @areaCode varchar(5),      
 @number varchar(20),      
 @email varchar(100),      
 @middleName varchar(64) = null,      
 @admDegreeId int = null,      
 @dept varchar(64) = null,      
 @title varchar(64) = null,      
 @faxAreaCode varchar(5) = null,      
 @faxNumber varchar(20) = null,    
 @cOIRole  varchar(100)=null,    
 @isOutsideMember bit = 0,
 @HierarchyTypeId INT =3,
 @HierarchyTypeIdOld INT = 3,
 @ParentId INT = 0
)      
as      
begin      
      
set nocount on      
      
begin transaction UpdatePerson      
      
  declare @admEmailAddressTypeId int      
  declare @phoneTypeId int      
  declare @faxTypeId int      
      
  -- these are being set to the primary record types      
  set @admEmailAddressTypeId = 1       
  set @phoneTypeId  = 1      
  set @faxTypeId = 3      
      
  declare @errorMessage varchar(64)      
      
  -- this is a new person; do an insert      
  if (@admPersonId = 0)      
    BEGIN    
     -- this is an external person (internal person not editable right now)      
     if (@admPersonType = 2)      
     begin      
     insert into AdmPerson (LastName, FirstName, MiddleName, AdmPersonTypeId,Title,COIRole,OutsideMember)      
     values (@lastName, @firstName, @middleName, 2,@title,@cOIRole,@isOutsideMember)      
          
       if (@@error <> 0)      
       begin      
        set @errorMessage = 'Failed to Insert into AdmPerson'      
        goto ErrorHandler      
       end      
          
       set @admPersonId = ident_current('AdmPerson')      
       /*The title is added to the Admperson table(to be removed from the admexternalPerson).*/    
       insert into AdmExternalPerson (AdmPersonId, Department, Institution)      
       values (@admPersonId, @dept, @orgName)      
          
       if (@@error <> 0)      
       begin      
        set @errorMessage = 'Failed to Insert into AdmExternalPerson'      
        goto ErrorHandler      
       end      
          
       if (@admDegreeId is not null)      
       insert into AdmDegree_AdmPerson (AdmPersonId, AdmDegreeId)      
       values (@admPersonId, @admDegreeId)      
          
       if (@@error <> 0)      
       begin      
        set @errorMessage = 'Failed to Insert into AdmDegree_AdmPerson'      
        goto ErrorHandler      
       end      
          
       insert into AdmEmailAddress (EmailAddress, AdmPersonId, AdmEmailAddressTypeId)      
       values (@email, @admPersonId, @admEmailAddressTypeId)      
          
       if (@@error <> 0)      
       begin      
        set @errorMessage = 'Failed to Insert into AdmEmailAddress'      
        goto ErrorHandler      
       end      
          
       insert into AdmTelephoneNumber (CountryCode, AreaCode, Number, AdmPersonId, AdmTelephoneTypeId)      
       values (0, @areaCode, @number, @admPersonId, @phoneTypeId)      
          
       if (@@error <> 0)      
       begin      
        set @errorMessage = 'Failed to Insert phone number into AdmTelephoneNumber'      
     goto ErrorHandler      
       end      
          
       if (@faxNumber is not null)      
       begin      
        insert into AdmTelephoneNumber (CountryCode, AreaCode, Number, AdmPersonId, AdmTelephoneTypeId)      
        values (0, @faxAreaCode, @faxNumber, @admPersonId, @faxTypeId)      
           
        if (@@error <> 0)      
        begin      
      set @errorMessage = 'Failed to Insert Fax Number into AdmTelephoneNumber'      
      goto ErrorHandler      
        end      
       end        
          
       commit transaction UpdatePerson      
       select @admPersonId as 'AdmPersonId'      
       return      
     end      
    end    
 Else    
  BEGIN    

	Update AdmPerson Set COIRole=@cOIRole, OutsideMember=@isoutsideMember Where Id=@admPersonId   
 	if (@@error <> 0)      
       	begin      
        	set @errorMessage = 'Failed to Insert phone number into AdmTelephoneNumber'      
        	goto ErrorHandler      
	end  
    
    --Updating the COI_Hierarchy_Map table's HeirarchyTypeId
    UPDATE COI_Hierarchy_Map 
	SET 
	HierarchyTypeId = @HierarchyTypeId 
	WHERE 
	ParentId = @parentID 
	AND ChildId = @admPersonId 
	AND HierarchyTypeId = @HierarchyTypeIdOld
	
     
    commit transaction UpdatePerson      
       select @admPersonId as 'AdmPersonId'      
       return      
  END    
    
   ErrorHandler:       
   begin      
    rollback transaction UpdatePerson      
    raiserror (@errorMessage, 16, 1)      
    return      
   end      
        
end      
